<?

/*
*
*   Excalibur Content Management System
*   Copyright © 2008 Egor "Sontan" Kuryanovich
*
*   Based on Explay Engine v2.0 by Golovdinov Alexander
*
*   Official site: www.excms.ru
*   Contact e-mail: support@excms.ru
*
*   GNU General Public License original source:
*   http://www.gnu.org/licenses/gpl-3.0.html
*
*/

if ($EXCMS != "work") {
	exit;
}

if (!file_exists ("modules/auth/admin/fields_options.txt")) write_options ("","","","","","","","","","","","","","","","");

if (isset($_GET['save'])) {
	$string = "";

	(isset($_POST['icq_active'])) ? $string .= "icq|on\r\n" : $string .= "icq|off\r\n";
	(isset($_POST['http_active'])) ? $string .= "http|on\r\n" : $string .= "http|off\r\n";
	(isset($_POST['avatar_active'])) ? $string .= "avatar|on\r\n" : $string .= "avatar|off\r\n";
	(isset($_POST['friends_active'])) ?$string .= "friends|on\r\n" : $string .= "friends|off\r\n";
	(isset($_POST['karma_active'])) ? $string .= "karma|on\r\n" : $string .= "karma|off\r\n";
	(isset($_POST['arts_active'])) ? $string .= "arts|on\r\n" : $string .= "arts|off\r\n";
	(isset($_POST['comments_active'])) ? $string .= "comments|on\r\n" : $string .= "comments|off\r\n";
	
	// name | display | type | value (not for all) | label
	for ($i=1; $i<=9; $i++) {
		if (isset ($_POST['ad'.$i.'_active'])) {
			(isset ($_POST['ad'.$i.'_important'])) ? $important = "on" : $important = "off";
			$_POST['ad'.$i.'_value'] = str_replace ("\n", "<br>", $_POST['ad'.$i.'_value']);
			$_POST['ad'.$i.'_value'] = str_replace ("\r", "", $_POST['ad'.$i.'_value']);
			$string .= "ad".$i."|on|".$_POST['ad'.$i.'_type']."|".$_POST['ad'.$i.'_value']."|".$_POST['ad'.$i.'_label']."|$important\r\n";
		} else {
			$string .= "ad".$i."|off||||off\r\n";
		}
	}
	
	$file = fopen ("modules/auth/admin/fields_options.txt", "w+");
	fwrite ($file, $string);
	fclose ($file);
	
	site_message ("Настройки сохранены!");
}


$file = file ("modules/auth/admin/fields_options.txt");
$field = array ();

// 0 - name
// 1 - status
// 2 - type
// 3 - value (not for all)
// 4 - label
// 5 - important

foreach ($file as $f) {
	$fld = explode ("|", $f);
	//print "{$fld[0]}|{$fld[1]}|{$fld[2]}|{$fld[3]}|{$fld[4]}|{$fld[5]}<br>";
	(isset($fld[2])) ? $field[$fld[0]] = $fld : $field[$fld[0]] = gen_checkbox($fld[0], trim($fld[1]));
}

$form = "<form action=\"admin.php?name=users&act=fields&save=true\" method=\"post\">
<table class=\"main_text\" width=\"100%\">
	<tr><td colspan=\"4\"><strong>Стандартные поля:</strong></td></tr>
	<tr>
		<td valign=\"top\" width=\"4%\">{$field['icq']}</td>
		<td valign=\"top\" colspan=\"3\">ICQ</td>
	</tr>
	<tr>
		<td valign=\"top\">{$field['http']}</td>
		<td valign=\"top\" colspan=\"3\">Сайт</td>
	</tr>
	<tr>
		<td valign=\"top\">{$field['avatar']}</td>
		<td valign=\"top\" colspan=\"3\">Аватар</td>
	</tr>
	<tr>
		<td valign=\"top\">{$field['friends']}</td>
		<td valign=\"top\" colspan=\"3\">Друзья</td>
	</tr>
	<tr>
		<td valign=\"top\">{$field['karma']}</td>
		<td valign=\"top\" colspan=\"3\">Карма (рейтинг)</td>
	</tr>
	<tr>
		<td valign=\"top\">{$field['arts']}</td>
		<td valign=\"top\" colspan=\"3\">Блок последних статей</td>
	</tr>
	<tr>
		<td valign=\"top\">{$field['comments']}</td>
		<td valign=\"top\" colspan=\"3\">Комментарии к профилю</td>
	</tr>
	<tr><td colspan=\"4\"><br /><br /><strong>Дополнительные поля:</strong></td></tr>
	<tr>
		<td width=\"4%\"></td>
		<td width=\"20%\" align=\"center\"><strong>Название</strong></td>
		<td width=\"12%\" align=\"center\"><strong>Тип</strong></td>
		<td width=\"25%\" align=\"center\"><strong>Значение</strong></td>
		<td width=\"4%\"><strong>Обяз.</strong></td>
		<td></td>
	</tr>";
for ($i=1; $i<=9; $i++) {
	$field['ad'.$i][3] = str_replace ("<br>", "\r\n", $field['ad'.$i][3]);
	$form .= "\n<tr>
		<td valign=\"top\">".gen_checkbox ("ad$i", trim($field['ad'.$i][1]))."</td>
		<td valign=\"top\"><input type=\"text\" style=\"width:100%\" name=\"ad{$i}_label\" value=\"{$field['ad'.$i][4]}\" /></td>
		<td valign=\"top\">".gen_fieldtype ("ad$i", $field['ad'.$i][2])."</td>
		<td valign=\"top\"><textarea rows=\"4\" cols=\"25\" name=\"ad{$i}_value\">{$field['ad'.$i][3]}</textarea></td>
		<td valign=\"top\">".gen_checkbox ("ad$i", trim($field['ad'.$i][5]), "important")."</td>
		<td></td>
	</tr>";
}
$form .= "\n<tr><td colspan=\"4\"><input type=\"submit\" value=\"Сохранить\" /></td></tr>
</table>
</form>";

past_table ("Управление полями", $form);

function gen_checkbox ($name, $arg, $pref = "active") {
	if ($arg == "on") return "<center><input type=\"checkbox\" name=\"{$name}_$pref\" value=\"on\" checked /></center>";
	else return "<center><input type=\"checkbox\" name=\"{$name}_$pref\" value=\"on\" /></center>";
}

function gen_fieldtype ($name, $arg) {
	$types = array ("text", "textarea", "select", "radio", "checkbox");
	$s = "\n<center><select name=\"{$name}_type\">";
	foreach ($types as $type) {
		($type == $arg) ? $s .= "\n\t<option selected>$type</option>" : $s .= "\n\t<option>$type</option>";
	}
	$s .= "\n</select></center>";
	return $s;
}

function write_options ($icq, $hhtp, $avatar, $friends, $karma, $communities, $comments, $ad1, $ad2, $ad3, $ad4, $ad5, $ad6, $ad7, $ad8, $ad9) {

	if ($icq == "") $icq = "on";
	if ($hhtp == "") $hhtp = "on";
	if ($avatar == "") $avatar = "on";
	if ($friends == "") $friends = "off";
	if ($karma == "") $karma = "off";
	if ($communities == "") $communities = "off";
	if ($comments == "") $comments = "off";
	
	if ($ad1 == "") $ad1 = array ("off", "", "", "", "off");
	if ($ad2 == "") $ad2 = array ("off", "", "", "", "off");
	if ($ad3 == "") $ad3 = array ("off", "", "", "", "off");
	if ($ad4 == "") $ad4 = array ("off", "", "", "", "off");
	if ($ad5 == "") $ad5 = array ("off", "", "", "", "off");
	if ($ad6 == "") $ad6 = array ("off", "", "", "", "off");
	if ($ad7 == "") $ad7 = array ("off", "", "", "", "off");
	if ($ad8 == "") $ad8 = array ("off", "", "", "", "off");
	if ($ad9 == "") $ad9 = array ("off", "", "", "", "off");

	$c = "icq|$icq\r\n";
	$c .= "http|$http\r\n";
	$c .= "avatar|$avatar\r\n";
	$c .= "friends|$friends\r\n";
	$c .= "karma|$karma\r\n";
	$c .= "arts|$communities\r\n";
	$c .= "comments|$comments\r\n";
	
	// name | display | type | value (not for all) | label
	$c .= "ad1|".$ad1[0]."|".$ad1[1]."|".$ad1[2]."|".$ad1[3]."|".$ad1[4]."\r\n";
	$c .= "ad2|".$ad2[0]."|".$ad2[1]."|".$ad2[2]."|".$ad2[3]."|".$ad2[4]."\r\n";
	$c .= "ad3|".$ad3[0]."|".$ad3[1]."|".$ad3[2]."|".$ad3[3]."|".$ad3[4]."\r\n";
	$c .= "ad4|".$ad4[0]."|".$ad4[1]."|".$ad4[2]."|".$ad4[3]."|".$ad4[4]."\r\n";
	$c .= "ad5|".$ad5[0]."|".$ad5[1]."|".$ad5[2]."|".$ad5[3]."|".$ad5[4]."\r\n";
	$c .= "ad6|".$ad6[0]."|".$ad6[1]."|".$ad6[2]."|".$ad6[3]."|".$ad6[4]."\r\n";
	$c .= "ad7|".$ad7[0]."|".$ad7[1]."|".$ad7[2]."|".$ad7[3]."|".$ad7[4]."\r\n";
	$c .= "ad8|".$ad8[0]."|".$ad8[1]."|".$ad8[2]."|".$ad8[3]."|".$ad8[4]."\r\n";
	$c .= "ad9|".$ad9[0]."|".$ad9[1]."|".$ad9[2]."|".$ad9[3]."|".$ad9[4]."\r\n";
	
	$file = fopen ("modules/auth/admin/fields_options.txt", "w+");
	fwrite ($file, $c);
	fclose ($file);

}
